/*
 * Copyright @ 苏州瑞云信息技术有限公司 All rights reserved.
 */
package work.base.data.date.util;

import cn.hutool.core.date.DateUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * @author Sakura Shen
 *
 * java.util 包 关于日期时间的基本使用
 * Date 对象是所有时间封装类的基础
 * Calendar的使用
 *
 *
 */
public class DateTest {
    public static void main(String[] args) throws ParseException {
        DateTest dateTest = new DateTest();
        dateTest.calendarCompare();
    }

    public void main0() throws ParseException {
        // 1 当前时间
        Date date = new Date();
        System.out.println(date);

        // 2 构造函数接收一个参数，该参数是从1970年1月1日起的毫秒数。
        System.out.println(System.currentTimeMillis());
        Date date1 = new Date(1000L);
        System.out.println(date1);

        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.getTime());

        // 日期/时间格式化子类的抽象类
        DateFormat dateFormat = new SimpleDateFormat();
        DateFormat df = DateFormat.getDateInstance();


        // java.util
        // Date转 String
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println("当前时间为: " + ft.format(new Date()));
        // String 转 Date
        System.out.println(ft.parse("2021-07-31 11:47:33"));


        Calendar c1 = Calendar.getInstance();
        c1.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
        c1.add(Calendar.DAY_OF_YEAR, 2);
        System.out.println(c1.getTime());
        System.out.println(c1.get(Calendar.DAY_OF_WEEK));
        System.out.println(c1.get(Calendar.DAY_OF_MONTH));
        System.out.println(c1.get(Calendar.DAY_OF_YEAR));
        System.out.println(c1.get(Calendar.DAY_OF_WEEK_IN_MONTH));

        Calendar c2 = new GregorianCalendar();
        System.out.println(c2.getTimeZone().getID());


        Date localDate = new Date();
        /*根据洛杉矶的时区ID得到时区实例 */
        TimeZone usaZone = TimeZone.getTimeZone("Japan");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(usaZone);
        System.out.println(sdf.getTimeZone());
        String usaDateString = sdf.format(localDate);
        System.out.println(usaDateString);
        System.out.println(sdf.parse(usaDateString));

        // 设置时区 后续的时间都变了
        TimeZone.setDefault(TimeZone.getTimeZone("Japan"));
        System.out.println("时区变化之后的时间：" + new Date());

        System.out.println("END===");
    }

    public void compareDate() {
        Date date1 = DateUtil.parse("2022-01-03").toJdkDate();
        Date date2 = DateUtil.parse("2022-01-03").toJdkDate();
        int res = date1.compareTo(date2);
        System.out.println(res);
    }


    public void calendarCompare() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2022, Calendar.NOVEMBER, 1, 0, 0, 0);
        Date date1 = calendar.getTime();
        Date date2 = DateUtil.parseDate("2022-11-01").toJdkDate();

        // Calendar构造的Date 和 使用字符串构造的Date不相等
        System.out.println(date1.equals(date2));
    }
}
